﻿/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DaveSexton.DocProject.Sandcastle.Configuration;

namespace DaveSexton.DocProject.Sandcastle
{
	public sealed partial class ImportSettingsControl : UserControl
	{
		#region Public Properties
		[Browsable(false)]
		public Presentation Presentation
		{
			get
			{
				return presentation;
			}
		}
		#endregion

		#region Private / Protected
		private readonly Presentation presentation;
		private bool projectTextBoxDirty;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ImportSettingsControl" /> class.
		/// </summary>
		public ImportSettingsControl(Presentation presentation)
			: this()
		{
			if (presentation == null)
				throw new ArgumentNullException("presentation");

			this.presentation = presentation;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ImportSettingsControl" /> class.
		/// </summary>
		private ImportSettingsControl()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		#endregion

		#region Events
		private readonly object PresentationChangedEvent = new object();

		/// <summary>
		/// Event raised when the <see cref="Presentation"/> property changes.
		/// This event is not raised when the property value changes, only when the object's state changes.
		/// </summary>
		[Category("Property Changed")]
		[Description("Event raised when the Presentation property changes.")]
		public event EventHandler PresentationChanged
		{
			add
			{
				lock (PresentationChangedEvent)
				{
					Events.AddHandler(PresentationChangedEvent, value);
				}
			}
			remove
			{
				lock (PresentationChangedEvent)
				{
					Events.RemoveHandler(PresentationChangedEvent, value);
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="PresentationChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
		private void OnPresentationChanged(EventArgs e)
		{
			EventHandler handler = null;

			lock (PresentationChangedEvent)
			{
				handler = (EventHandler) Events[PresentationChangedEvent];
			}

			if (handler != null)
				handler(this, e);
		}
		#endregion

		#region Event Handlers
		protected override void OnValidating(CancelEventArgs e)
		{
			e.Cancel = !ValidateChildren();
			
			base.OnValidating(e);
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
		private void projectTextBox_Validating(object sender, CancelEventArgs e)
		{
			if (projectTextBox.Text.Trim().Length > 0)
			{
				if (projectTextBoxDirty)
				{
					try
					{
						SystemPath file = new SystemPath(projectTextBox.Text, false, false);

						Microsoft.Build.BuildEngine.Engine engine = new Microsoft.Build.BuildEngine.Engine(
							System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory());

						presentation.ImportFrom = new DaveSexton.DocProject.MSBuild.MSBuildDocProject(engine, file);

						errorProvider1.SetError(projectTextBox, null);

						OnPresentationChanged(EventArgs.Empty);
					}
					catch (Exception ex)
					{
						errorProvider1.SetError(projectTextBox, ex.Message);
						e.Cancel = true;
					}

					projectTextBoxDirty = false;
				}
			}
			else
			{
				errorProvider1.SetError(projectTextBox, null);
				presentation.ImportFrom = null;

				OnPresentationChanged(EventArgs.Empty);
			}
		}

		private void projectTextBox_TextChanged(object sender, EventArgs e)
		{
			projectTextBoxDirty = true;
			errorProvider1.SetError(projectTextBox, null);
		}

		private void browseButton_Click(object sender, EventArgs e)
		{
			using (OpenFileDialog dialog = new OpenFileDialog())
			{
				dialog.Title = Resources.Text.ProjectOpenFileDialogTitle;
				dialog.Filter = "All DocProjects and DocSites (*.vbproj;*.csproj;*.vjsproj)|*.vbproj;*.csproj;*.vjsproj" +
					"|Visual Basic Projects (*.vbproj)|*.vbproj" +
					"|Visual C# Projects (*.csproj)|*.csproj" +
					"|Visual J# Projects (*.vjsproj)|*.vjsproj";

				if (dialog.ShowDialog(FindForm()) == DialogResult.OK)
				{
					projectTextBox.Text = dialog.FileName;
				}
			}
		}
		#endregion
	}
}
